package util.content;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ContentCapture {

	// 抓取字符串中的有效内容部分
	public String getContent(String contentBodyString) {
		final String preString = "<div><!--woshizhushistart --><div>";
		final String trailString = "</div><!--woshizhushiend --></div>";
		//final String mainAskBeginning = "<div><h4 > 参考答案 </h4>";
		String finalContent = "";
		String contentOneContentString = null;
		RandomContent rc = new RandomContent();
		
		// FilterSpace fs = new FilterSpace();
		// 把问题、最佳回答、普通回答分开了
		Pattern contentAreaPattern = Pattern.compile(contentArea);
		Matcher contentAreaMatcher = contentAreaPattern.matcher(contentBodyString);
		contentAreaMatcher.matches();
		contentAreaMatcher.reset();
		while (contentAreaMatcher.find()) {
			// contentAreaString——问或答、最佳的一个区块的字符串
			String contentAreaString = contentAreaMatcher.group();
			// 最佳回答、普通回答可能包含了7多个回答，多个回答又需要分开
			// System.out.println("contentAreaString");
			Pattern oneContentAreaPattern = Pattern.compile(oneContentArea);
			Matcher oneContentAreaMatcher = oneContentAreaPattern.matcher(contentAreaString);
			oneContentAreaMatcher.matches();
			oneContentAreaMatcher.reset();
			while (oneContentAreaMatcher.find()) {
				String oneQA = "";
				// System.out.println("--------------------contentAreaMatcher");
				// 拿到了一个回答包括下面的追问追答的字符串
				contentOneContentString = oneContentAreaMatcher.group();
				// 抓取出主回答不包括追问追答的部分
				Pattern mainAnswerAreaPattern = Pattern.compile(mainArea);
				Matcher mainAnswerAreaMatcher = mainAnswerAreaPattern.matcher(contentOneContentString);
				mainAnswerAreaMatcher.matches();
				mainAnswerAreaMatcher.reset();
				String mainAnswerAreaString = "";
				while (mainAnswerAreaMatcher.find()) {
					// System.out.println("--------------------mainAnswerAreaMatcher");
					mainAnswerAreaString = mainAnswerAreaMatcher.group();
				}
				//System.out.println(contentdel);
				Pattern toLinePatternAnswer = Pattern.compile(mainRex);
				Matcher toLineMatcherAnswer = toLinePatternAnswer.matcher(mainAnswerAreaString);
				toLineMatcherAnswer.matches();
				toLineMatcherAnswer.reset();
				while (toLineMatcherAnswer.find()) {
					// System.out.println("--------------------toLineMatcherAnswer");
					//这一样以替换的方式过滤和简化了很多无用的标签字符串
					String oneLineString = toLineMatcherAnswer.group().replaceAll(contentdel, "").replaceAll("<pre.*>\\s*", "<pre>").replaceAll("<div.*>", "<div>").replaceAll("<br/>\\W*<br/>", "<br/>").trim() + "\n";
					if (contentAttri.length() > 3) {
						oneLineString = "<div>" + oneLineString + "</div>";
					}
					// System.out.println(oneLineString);
					oneQA = oneQA + oneLineString;

				}

				if (contentAttri.length() > 3) {
					// System.out.println("<h4>参考答案</h4>");
					oneQA = preString + oneQA + trailString;
				}

				rc.contentAdd(oneQA + "\n" + "\n");
			}

		}

		// 每段内容加一个换行和分割线
		finalContent = rc.getWycContent();

		// System.out.println(finalContent);
		rc.clear();
		return finalContent;
	}

	String contentdel = null;

	public void setContentdel(String contentdel) {
		this.contentdel = contentdel;
		// System.out.println(contentdel);
	}

	String contentArea = null;

	public void setContentArea(String contentArea) {
		this.contentArea = contentArea;
		// System.out.println(contentArea);
	}

	String oneContentArea = null;

	public void setOneContentArea(String oneContentArea) {
		this.oneContentArea = oneContentArea;
		// System.out.println(oneContentArea);
	}

	String mainArea = null;

	public void setMainArea(String mainArea) {
		this.mainArea = mainArea;
		// System.out.println(mainArea);
	}

	String mainRex = null;

	public void setMainRex(String mainRex) {
		this.mainRex = mainRex;
	}

	String contentAttri = null;

	public void setContentAttri(String contentAttri) {
		this.contentAttri = contentAttri;
	}

}
